/**
 * 将对象的键从下划线转换为驼峰
 */
export const toCamelCase = (obj: any): any => {
  if (Array.isArray(obj)) {
    return obj.map(v => toCamelCase(v));
  } else if (obj !== null && obj!==undefined && obj.constructor === Object) {
    return Object.keys(obj).reduce(
      (result, key) => ({
        ...result,
        [snakeToCamel(key)]: toCamelCase(obj[key])
      }),
      {}
    );
  }
  return obj;
};

/**
 * 将对象的键从驼峰转换为下划线
 */
export const toSnakeCase = (obj: any): any => {
  if (Array.isArray(obj)) {
    return obj.map(v => toSnakeCase(v));
  } else if (obj !== null && obj!==undefined && obj.constructor === Object) {
    return Object.keys(obj).reduce(
      (result, key) => ({
        ...result,
        [camelToSnake(key)]: toSnakeCase(obj[key])
      }),
      {}
    );
  }
  return obj;
};

/**
 * 将字符串从下划线命名转换为驼峰命名
 */
const snakeToCamel = (str: string): string =>
  str.toLowerCase().replace(/([-_][a-z])/g, group =>
    group.toUpperCase().replace('-', '').replace('_', '')
  );

/**
 * 将字符串从驼峰命名转换为下划线命名
 */
const camelToSnake = (str: string): string =>
  str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
